﻿// ==================================================
// Copyright 2014 (C) , dotLogix
// Author: Alexander Schill <alexander@schillnet.de>.
// ==================================================

namespace DotLogix {
    #region Using Directives
    using System;
    using System.Globalization;
    using System.Windows;
    using System.Windows.Media;

    #endregion

    public class AutoSizeText : AutoSizeBase {
        private FontFamily _fontFamily = new FontFamily();
        private double _fontSize = 10;
        private FontStretch _fontStretch;
        private FontStyle _fontStyle;
        private FontWeight _fontWeight;
        private Brush _foreground = Brushes.Black;
        private FormattedText _formattedText;
        private string _text = string.Empty;

        public AutoSizeText() {
            VerticalAlignment = VerticalAlignment.Center;
            HorizontalAlignment = HorizontalAlignment.Center;
        }

        public Brush Foreground {
            get { return _foreground; }
            set {
                _foreground = value;
                _formattedText.SetForegroundBrush(value);
                InvalidateVisual();
            }
        }

        public FontFamily FontFamily {
            get { return _fontFamily; }
            set {
                _fontFamily = value;
                _formattedText.SetFontFamily(value);
                InvalidateMeasure();
                InvalidateVisual();
            }
        }

        public FontStyle FontStyle {
            get { return _fontStyle; }
            set {
                _fontStyle = value;
                _formattedText.SetFontStyle(value);
                InvalidateMeasure();
                InvalidateVisual();
            }
        }

        public FontWeight FontWeight {
            get { return _fontWeight; }
            set {
                _fontWeight = value;
                _formattedText.SetFontWeight(value);
                InvalidateMeasure();
                InvalidateVisual();
            }
        }

        public FontStretch FontStretch {
            get { return _fontStretch; }
            set {
                _fontStretch = value;
                _formattedText.SetFontStretch(value);
                InvalidateMeasure();
                InvalidateVisual();
            }
        }

        public double FontSize {
            get { return _fontSize; }
            set {
                _fontSize = value;
                _formattedText.SetFontSize(value);
                InvalidateMeasure();
                InvalidateVisual();
            }
        }

        public string Text {
            get { return _text; }
            set {
                _text = value;
                _formattedText = new FormattedText(Text, CultureInfo.CurrentCulture, FlowDirection,
                    new Typeface(FontFamily, FontStyle, FontWeight, FontStretch), FontSize, Foreground);
                InvalidateMeasure();
                InvalidateVisual();
            }
        }

        protected override Size Content { get { return new Size(_formattedText.Width,_formattedText.Height); } }

        protected override void RenderContent(DrawingContext drawingContext) {
            drawingContext.DrawText(_formattedText, new Point());
        }
    }
}